package net.enclosing.basecamp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

public class Milestone {
	String id = "";
	boolean completed = false;
	String name = "";
	Date deadline  = null;
	int projectid = 0;
	int responsibleParty = 0;
	Project project = null;
	public String toString(){
		StringBuilder builder = new StringBuilder();
		builder.append(" - ");
		builder.append(id);
		builder.append(name);
		builder.append(deadline);
		builder.append(project!=null?project.getName():" nil,,, ");
		builder.append("\r\n");
//		builder.append("<br />");
		return builder.toString();
		
	}
	public static void main(String[] args) {
		try {
			List<Milestone> milestones = Milestone.recents();
			for (Milestone milestone : milestones) {
				System.err.println(milestone.toString());
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static List<Milestone> recents() throws ParseException {
		List<Project> activeProjects = Project.actives();
		ArrayList<Milestone> milestones = new ArrayList<Milestone>();
		for (Project project : activeProjects) {
			List<Milestone> milestonesOfAProject = Milestone.recentsOf(project);
			milestones.addAll(milestonesOfAProject);
		}
		return milestones;
	}
	public static List<Milestone> recentsOf(Project p) throws ParseException {
		LocalStaticData staticData = new LocalStaticData();
		String path = "projects/" + p.getId() + "/milestones/list.xml";
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Document document =  Basecamp.get(path);
		Element milestonesElement =  document.getRootElement();
		List<Element> milestoneElements = milestonesElement.elements();
		ArrayList<Milestone> milestones = new ArrayList<Milestone>();
		Date recentThresholdDate = Basecamp.recentThresholdDate(); 
		for (Element milestoneElement : milestoneElements) {
			Milestone milestone = new Milestone();
//			System.err.println(milestoneElement.asXML());
//			System.err.println(milestoneElement.element("completed").getText().equals("false"));
//			System.err.println(milestoneElement.element("responsible-party-id").getText().equals(String.valueOf(staticData.getUserid())));
//			System.err.println(dateFormat.parse(milestoneElement.element("deadline").getText()).before(recentThresholdDate));
			try {
				if(milestoneElement.element("completed").getText().equals("false") //not copleted, and 
						&& milestoneElement.element("responsible-party-id").getText().equals(String.valueOf(staticData.getUserid()))
						&& dateFormat.parse(milestoneElement.element("deadline").getText()).before(recentThresholdDate)
						){
					milestone.setCompleted(true);
					milestone.setId(milestoneElement.elementText("id"));
					milestone.setName(milestoneElement.elementText("title"));
					milestone.setDeadline(dateFormat.parse(milestoneElement.elementText("deadline")));
					milestone.setProjectid(Integer.valueOf(milestoneElement.elementText("project-id")).intValue());
					milestone.setResponsibleParty(staticData.getUserid());
					milestone.setProject(p);
					milestones.add(milestone);
				}
				
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return milestones;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public boolean isCompleted() {
		return completed;
	}
	public void setCompleted(boolean completed) {
		this.completed = completed;
	}
	public Date getDeadline() {
		return deadline;
	}
	public void setDeadline(Date deadline) {
		this.deadline = deadline;
	}
	
	public int getProjectid() {
		return projectid;
	}
	public void setProjectid(int projectid) {
		this.projectid = projectid;
	}
	public int getResponsibleParty() {
		return responsibleParty;
	}
	public void setResponsibleParty(int responsibleParty) {
		this.responsibleParty = responsibleParty;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Project getProject() {
		return project;
	}
	public void setProject(Project project) {
		this.project = project;
	}
	
	
	
}
